package com.uduew.sjd.ui.mine

import android.content.Context
import android.os.Bundle
import android.view.View
import com.uduew.sjd.BR
import com.uduew.sjd.R
import com.uduew.sjd.config.AppConfig
import com.uduew.sjd.message.Messages
import com.uduew.sjd.message.PageMessenger
import com.uduew.sjd.request.ProductInfoRequester
import com.uduew.sjd.ui.base.BaseFragment
import com.uduew.sjd.ui.base.StateHolder
import com.uduew.sjd.utils.Utils
import com.hjq.bar.OnTitleBarListener
import com.hjq.bar.TitleBar
import com.kongzue.dialogx.dialogs.MessageDialog
import com.kongzue.dialogx.dialogs.TipDialog
import com.kongzue.dialogx.interfaces.OnDialogButtonClickListener
import com.kunminx.architecture.ui.page.DataBindingConfig
import com.kunminx.architecture.ui.state.State
import com.uduew.sjd.config.Const
import com.uduew.utils.AppUtils
import java.io.File

class SettingFragment : BaseFragment() {

    lateinit var mState: SettingStates
    private lateinit var mProductInfoRequester: ProductInfoRequester
    private lateinit var mMessenger: PageMessenger

    override fun initViewModel() {
        mState = getFragmentScopeViewModel(SettingStates::class.java)
        mProductInfoRequester = getFragmentScopeViewModel(ProductInfoRequester::class.java)
        mMessenger = getApplicationScopeViewModel(PageMessenger::class.java)

    }


    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        //设置版本号
        val version = AppUtils.getAppVersionName()
        mState.version.set(version);
    }

    override fun getDataBindingConfig(): DataBindingConfig {

        return DataBindingConfig(R.layout.fragment_setting, BR.vm, mState)
            .addBindingParam(BR.click, ClickProxy())
            .addBindingParam(BR.titleBarListener, TitleBarListener())
    }


    inner class TitleBarListener : OnTitleBarListener {
        override fun onLeftClick(titleBar: TitleBar?) {
            super.onLeftClick(titleBar)
            if (!Utils.checkDebounce()) return
            nav().popBackStack()
        }

        override fun onRightClick(titleBar: TitleBar?) {
            super.onRightClick(titleBar)
        }
    }

    class SettingStates : StateHolder() {
        val text: State<String> = State("set")
        val version: State<String> = State("0")
    }

    // 清除内部缓存
    fun clearAppCache(context: Context) {
        // 删除内部缓存
        context.cacheDir.deleteRecursively()
        // 删除外部缓存（可选）
        context.externalCacheDir?.deleteRecursively()
    }

    // 扩展函数：递归删除
    fun File.deleteRecursively(): Boolean {
        return if (isDirectory) {
            listFiles()?.all { it.deleteRecursively() } ?: false && delete()
        } else {
            delete()
        }
    }

    fun logOutAction() {
        if (!Utils.checkDebounce()) return
        MessageDialog.show(getString(R.string.hint), getString(R.string.do_you_want_to_log_out))
            .setCancelButton(R.string.cancel)
            .setOkButton(getString(R.string.sure))
            .setCancelButtonClickListener(object : OnDialogButtonClickListener<MessageDialog?> {
                override fun onClick(messageDialog: MessageDialog?, view: View?): Boolean {
                    return false
                }
            }).setOkButtonClickListener(object : OnDialogButtonClickListener<MessageDialog?> {
                override fun onClick(messageDialog: MessageDialog?, view: View?): Boolean {
                    AppConfig.isLogin = false
                    AppConfig.mConfigs.token().set(Const.API_UN_LOGIN)
                    nav().popBackStack()
                    mMessenger.input(Messages(Messages.EVENT_LOGIN_OUT_SUCCESS))
                    return false
                }
            })
    }

    private fun cleanAction() {
        this@SettingFragment.clearAppCache(requireContext())
    }

    private fun logoutAndClean() {

        if (!Utils.checkDebounce()) return
        MessageDialog.show(getString(R.string.hint), getString(R.string.whether_to_clear_the_cache))
            .setCancelButton(getString(R.string.cancel))
            .setOkButton(getString(R.string.sure))
            .setCancelButtonClickListener(object : OnDialogButtonClickListener<MessageDialog?> {
                override fun onClick(messageDialog: MessageDialog?, view: View?): Boolean {
                    return false
                }
            }).setOkButtonClickListener(object : OnDialogButtonClickListener<MessageDialog?> {
                override fun onClick(messageDialog: MessageDialog?, view: View?): Boolean {
                    logOutAction()
                    cleanAction()
                    return false
                }
            })


    }

    open inner class ClickProxy {

        fun logOut() {
            logOutAction()
        }

        fun clean() {
            logoutAndClean()
        }
    }

}